/**
 * this class is The Commands class that performs the commands from the observer
 *
 */
package Command;
/*this class and only this well write to the Log!!*/

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;

import Log.Log;
import Algo.Algo;
import DataBase.MenuTag_File;


/**
* Description : the class to connect between the DB(of file and Tags) to the observing
* <p>
*
*@author Yeala and Danielle 
*/
public class DirectCommand {
	
	private ArrayList<Algo>  algoList ;
	private MenuTag_File dataBase;
	
	/**
	* Description Ctor
	* @param ArrayList<Algo> algo
	* @param MenuTag_File dataBase
	*/
	public DirectCommand(ArrayList<Algo> algo,MenuTag_File dataBase)
	{
		this.dataBase=dataBase;
		algoList=algo;	
	}
	
	
	/**
	* Description - add file to tag it- change the path to DB - instead \ write \\
	* @param Path child
	*/
	public void addFile(Path child){//tag the file and then send it to the data base
		ArrayList<String>  tagList=new  ArrayList<String>();
		Iterator<Algo> algo_iter = algoList.iterator();	
		Algo algo;
		while (algo_iter.hasNext()){
			algo=algo_iter.next();
			String [] tagArray=algo.tagFile(child);
			if (tagArray!=null){
				for (int i=0;i<tagArray.length ;i++){
					tagList.add(tagArray[i]);
				}
			}
		}
		Iterator<String> tag_iter = tagList.iterator();
		String tag;
		String temp;
		boolean empty=true;
		while (tag_iter.hasNext()){
			tag=tag_iter.next();
			empty=false;
			new Log("file: "+child+" tagged in: "+tag);
			System.out.println("DirectCommand: file: "+child+" tag in: "+tag);
			/*---------- send to data base child and tag---------*/
			temp=child.toString();
			temp=temp.replaceAll("\\\\", "\\\\\\\\");
			dataBase.ConnectTag_File(temp, tag);
			
		}
		if (empty){
			new Log("file: "+child+" tagged in: no tags.");
			System.out.println("DirectCommand: file: "+child+" tag in: no tags.");
		}		
	}
	
	
	/**
	* Description Delete file from DB
	* @param Path child
	*/
	public void delFile(Path child){
		new Log("file: "+child+" deleted.");
		System.out.println("DirectCommand: del: "+child);
		String temp=child.toString();
		temp=temp.replaceAll("\\\\", "\\\\\\\\");
		dataBase.delFile(temp);
	}
}